package com.cmcc.pcap.voltePcapParse.pcap;


import com.cmcc.pcap.voltePcapParse.utils.pcap.DataUtils;

/**
 * IP 数据报头
 * @author starfish
 *
 */
public class IPHeader {

	/**
	 * 协议版本号(4 bit)及包头长度(4bit) =（1 字节）
	 * 版本号(Version):一般的值为0100（IPv4），0110（IPv6）
	 * IP包头最小长度为20字节
	 */
	private String varHLen;
	
	/**
	 * Type of  Service(Differentiated Services Field)：服务类型，（1 字节）
	 */
	private String tos;
	
	/**
	 * 总长度（2 字节）
	 */
	private short totalLen;
	
	/**
	 * 标识（2 字节）
	 */
	private String id;
	
	/**
	 * 标志与偏移量（2 字节）
	 */
	private short flagSegment;
	
	/**
	 * Time to Live：生存周期（1 字节）
	 */
	private int ttl;
	
	/**
	 * 协议类型（1 字节）
	 */
	private ProtocolType protocol;
	
	/**
	 * 头部校验和（2 字节）
	 */
	private String checkSum;
	
	/**
	 * 源 IP（4 字节）
	 */
	private String srcIP;
	
	/**
	 * 目的 IP（4 字节）
	 */
	private String dstIP;

	public String getVarHLen() {
		return varHLen;
	}

	public void setVarHLen(String varHLen) {
		this.varHLen = varHLen;
	}

	public String getTos() {
		return tos;
	}

	public void setTos(String tos) {
		this.tos = tos;
	}

	public short getTotalLen() {
		return totalLen;
	}

	public void setTotalLen(short totalLen) {
		this.totalLen = totalLen;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public short getFlagSegment() {
		return flagSegment;
	}

	public void setFlagSegment(short flagSegment) {
		this.flagSegment = flagSegment;
	}
	
	public short getFlags() {
		byte[] buff_2 = DataUtils.shortToByte2(flagSegment);
		short flag = (short)((buff_2[0] & 0xff)>>>5);
		return flag;
	}
	
	public short getSegment() {
		byte[] buff_2 = DataUtils.shortToByte2(flagSegment);
		short segment = (short)((((buff_2[0] & 0xff) << 3) & 0xff) | (buff_2[1] & 0xff));
		return segment;
	}

	public int getTtl() {
		return ttl;
	}

	public void setTtl(int ttl) {
		this.ttl = ttl;
	}

	public ProtocolType getProtocol() {
		return protocol;
	}

	public void setProtocol(ProtocolType protocol) {
		this.protocol = protocol;
	}

	public String getCheckSum() {
		return checkSum;
	}

	public void setCheckSum(String checkSum) {
		this.checkSum = checkSum;
	}

	public String getSrcIP() {
		return srcIP;
	}

	public void setSrcIP(String srcIP) {
		this.srcIP = srcIP;
	}

	public String getDstIP() {
		return dstIP;
	}

	public void setDstIP(String dstIP) {
		this.dstIP = dstIP;
	}
	
	public IPHeader() {	}
	
	@Override
	public String toString() {
		
		return "<b>Internet Protocol,Src:</b>"+srcIP+",<b>Dst:</b>"+dstIP+"<br>" 
		+ "&nbsp;&nbsp;<b>Version:</b>" + varHLen.charAt(0) 
		+ "<br>&nbsp;&nbsp;<b>Header Length:</b>" + (int)varHLen.charAt(1)*4+" bytes" 
		+ "<br>&nbsp;&nbsp;<b>Differentiated Services Field:</b>" + tos 
		+ "<br>&nbsp;&nbsp;<b>Total Length:</b>" + totalLen+"bytes"
		+ "<br>&nbsp;&nbsp;<b>Identification:</b>" + id
		+ "<br>&nbsp;&nbsp;<b>Flags:</b>" + DataUtils.shortToHexString2(getFlags())
		+ "<br>&nbsp;&nbsp;<b>Fragment offset:</b>" + getSegment()
		+ "<br>&nbsp;&nbsp;<b>Time to live(TTL):</b>" + ttl
		+ "<br>&nbsp;&nbsp;<b>Protocol:</b>" + protocol
		+ "<br>&nbsp;&nbsp;<b>Header checksum:</b>" + checkSum
		+ "<br>&nbsp;&nbsp;<b>Source Address:</b>" + srcIP
		+ "<br>&nbsp;&nbsp;<b>Destination Address:</b>" + dstIP;
	}
}
